using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class OutBoxDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        //public List<OutBox> GetOutBoxList()
        //{
        //    try
        //    {
        //        const string spName = "spo_OutBox_GetList";

        //        var OutBoxList = DbHelper.ExecuteSPList<OutBox>(spName);
        //        return OutBoxList;
        //    }
        //    catch (Exception exp)
        //    {
        //        LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

        //        return new List<OutBox>();
        //    }
        //}

        //public List<OutBox> GetOutBoxListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        //{
        //    try
        //    {
        //        const string spName = "spo_OutBox_GetByPaging";
        //        var sqlParams = new[]
        //                            {
        //                                new SqlParameter("@PageIndex", pageIndex),
        //                                new SqlParameter("@PageSize", pageSize),
        //                                new SqlParameter("@TotalRecord", totalRecord)
        //                            };

        //        SqlCommand sqlCommand = new SqlCommand(spName);
        //        sqlCommand.CommandType = CommandType.StoredProcedure;
        //        sqlCommand.CommandText = spName;
        //        sqlCommand.Parameters.AddRange(sqlParams);
        //        sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
        //        var OutBoxList = DbHelper.ExecuteSqlList<OutBox>(sqlCommand);
        //        if (sqlCommand.Parameters["@TotalRecord"].Value != null)
        //        {
        //            totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
        //        }

        //        return OutBoxList;
        //    }
        //    catch (Exception exp)
        //    {
        //        LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

        //        return new List<OutBox>();
        //    }
        //}

        //public OutBox GetOutBoxById(int boxId)
        //{
        //    try
        //    {
        //        const string spName = "spo_OutBox_GetById";
        //        var sqlParams = new[]
        //                            {
        //                                new SqlParameter("@BoxId", boxId)
        //                            };

        //        var OutBox = DbHelper.ExecuteSPList<OutBox>(spName, sqlParams).SingleOrDefault();
        //        return OutBox;
        //    }
        //    catch (Exception exp)
        //    {
        //        LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

        //        return null;
        //    }
        //}

        public int InsertOutBox(OutBox OutBox)
        {
            try
            {
                const string spName = "spi_OutBox_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@ItemId", OutBox.ItemId),
										new SqlParameter("@BoxCategory", OutBox.BoxCategory),
										new SqlParameter("@BoxImage", OutBox.BoxImage),
										new SqlParameter("@BoxType", OutBox.BoxType),
										new SqlParameter("@BoxId", OutBox.BoxId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@BoxId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@BoxId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@BoxId"].Value;
                }

				return -1;


            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int DeleteOutBox(int boxId)
        {
            try
            {
                const string spName = "spi_OutBox_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@BoxId", boxId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int OrderOutBox(int boxId, int boxcate, int action)
        {
            try
            {
                const string spName = "spi_OutBox_Order";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@BoxId", boxId),
                                    new SqlParameter("@BoxCate", boxcate),
                                    new SqlParameter("@Action", action)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int UpdateAvatar(int boxId, string avatar)
        {
            try
            {
                const string spName = "spi_OutBox_UpdateAvatar";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@BoxId", boxId),
                                    new SqlParameter("@BoxImage", avatar)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }
		#endregion
    }
}
